package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.ApplicationInfo;
import com.ruoyi.common.core.domain.entity.ApplicationUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.mapper.ApplicationInfoMapper;
import com.ruoyi.system.mapper.ApplicationUserMapper;
import com.ruoyi.system.service.IApplicationInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class ApplicationInfoServiceImpl implements IApplicationInfoService {

    private static final Logger log = LoggerFactory.getLogger(ApplicationInfoServiceImpl.class);

    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;

    @Autowired
    private ApplicationUserMapper applicationUserMapper;

    @Autowired
    protected Validator validator;

    @Override
    public List<ApplicationInfo> selectApplicationInfoList(ApplicationInfo info) {
        List<ApplicationInfo> applicationInfoList = applicationInfoMapper.selectApplicationInfoList(info);
        if (CollectionUtils.isNotEmpty(applicationInfoList)) {
            for (ApplicationInfo applicationInfo : applicationInfoList) {
                Long id = applicationInfo.getId();
                Integer alreadyTotal = applicationUserMapper.queryCountInfoOfUser(id);
                applicationInfo.setAlreadyTotal(alreadyTotal);
            }
        }
        return applicationInfoList;
    }

    @Override
    public List<ApplicationInfo> selectAllocatedList(ApplicationInfo user) {
        return null;
    }

    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importApplicationInfo(List<ApplicationInfo> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ApplicationInfo user : userList) {
            try {
                // 验证是否存在这个用户
                ApplicationInfo u = applicationInfoMapper.selectApplicationInfoByName(user.getName());
                if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, user);
                    user.setId(u.getId());
                    user.setUpdateBy(operName);
                    applicationInfoMapper.updateApplicationInfo(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public ApplicationInfo selectApplicationInfoByName(String name) {
        return applicationInfoMapper.selectApplicationInfoByName(name);
    }

    @Override
    public ApplicationInfo selectApplicationInfoById(Long userId) {
        return applicationInfoMapper.selectApplicationInfoById(userId);
    }

    @Override
    public int deleteApplicationInfoById(Long userId) {
        return applicationInfoMapper.deleteApplicationInfoById(userId);
    }

    @Override
    public int deleteApplicationInfoByIds(Long[] ids) {
        return applicationInfoMapper.deleteApplicationInfoByIds(ids);
    }

    @Override
    public int insertApplicationInfo(ApplicationInfo user) {
        int rows = applicationInfoMapper.insertApplicationInfo(user);
        return rows;
    }

    @Override
    public int updateApplicationInfo(ApplicationInfo user) {
        return applicationInfoMapper.updateApplicationInfo(user);
    }

    @Override
    public boolean checkEmailUnique(ApplicationInfo user) {
        return false;
    }

    @Override
    public int changeStatus(ApplicationInfo user) {
        return 0;
    }
}
